<template>
  <div class="home-page">
    <!-- 轮播图区域 -->
    <section class="hero-section">
      <a-carousel arrows autoplay>
        <template #prevArrow>
          <div class="custom-slick-arrow" style="left: 10px; z-index: 1">
            <left-circle-outlined />
          </div>
        </template>
        <template #nextArrow>
          <div class="custom-slick-arrow" style="right: 10px">
            <right-circle-outlined />
          </div>
        </template>
        <div>
          <a-image :src="carousel1" />
        </div>
        <div>
          <a-image :src="carousel2" />
        </div>
        <div>
          <a-image :src="carousel3" />
        </div>
        <div>
          <a-image :src="carousel4" />
        </div>
        <div>
          <a-image :src="carousel5" />
        </div>
      </a-carousel>
    </section>

    <!-- 欢迎信息 -->
    <section class="welcome-section">
      <div class="container">
        <h1 class="welcome-title">{{ msg }}</h1>
        <p class="welcome-subtitle">探索美丽风景，发现精彩路线</p>
      </div>
    </section>

    <!-- 推荐景点区域 -->
    <section class="recommend-section">
      <div class="container">
        <div class="section-header">
          <h2 class="section-title">
            <EnvironmentOutlined />
            推荐景点
          </h2>
          <a-button type="link" @click="viewMoreScenics">
            查看更多
            <RightOutlined />
          </a-button>
        </div>

        <div class="scenic-grid" v-if="recommendScenics.length > 0">
          <ScenicCard
            v-for="scenic in recommendScenics"
            :key="scenic.id"
            :scenic="scenic"
            @click="goToScenicDetail"
          />
        </div>

        <div class="empty-state" v-else-if="!loadingScenics">
          <a-empty description="暂无推荐景点" />
        </div>

        <div class="loading-state" v-if="loadingScenics">
          <a-spin size="large" />
        </div>
      </div>
    </section>

    <!-- 推荐路线区域 -->
    <section class="recommend-section">
      <div class="container">
        <div class="section-header">
          <h2 class="section-title">
            <CarOutlined />
            推荐路线
          </h2>
          <a-button type="link" @click="viewMoreRoutes">
            查看更多
            <RightOutlined />
          </a-button>
        </div>

        <div class="route-grid" v-if="recommendRoutes.length > 0">
          <RouteCard
            v-for="route in recommendRoutes"
            :key="route.id"
            :route="route"
            @click="goToRouteDetail"
          />
        </div>

        <div class="empty-state" v-else-if="!loadingRoutes">
          <a-empty description="暂无推荐路线" />
        </div>

        <div class="loading-state" v-if="loadingRoutes">
          <a-spin size="large" />
        </div>
      </div>
    </section>

    <!-- 景点详情模态框 -->
    <a-modal
      v-model:open="scenicDetailVisible"
      :title="selectedScenic?.scenicName"
      width="800px"
      :footer="null"
      centered
    >
      <ScenicDetailModal v-if="selectedScenic" :scenic="selectedScenic" />
    </a-modal>

    <!-- 路线详情模态框 -->
    <a-modal
      v-model:open="routeDetailVisible"
      :title="selectedRoute?.routeName"
      width="900px"
      :footer="null"
      centered
    >
      <RouteDetailModal v-if="selectedRoute" :route="selectedRoute" />
    </a-modal>
  </div>
</template>
<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import {
  LeftCircleOutlined,
  RightCircleOutlined,
  EnvironmentOutlined,
  CarOutlined,
  RightOutlined,
} from '@ant-design/icons-vue'
import carousel1 from '@/assets/xianrendong1.png'
import carousel2 from '@/assets/xianrendong2.png'
import carousel3 from '@/assets/xianrendong3.png'
import carousel4 from '@/assets/xianrendong4.png'
import carousel5 from '@/assets/xianrendong5.png'

import ScenicCard from '@/components/ScenicCard.vue'
import RouteCard from '@/components/RouteCard.vue'
import ScenicDetailModal from '@/components/ScenicDetailModal.vue'
import RouteDetailModal from '@/components/RouteDetailModal.vue'
import { getRecommendScenicList } from '@/api/scenicController'
import { getRecommendRouteList } from '@/api/routeController'

const msg = '欢迎来到仙人洞，从这里开始你的游玩之旅吧!'

const router = useRouter()

// 推荐数据
const recommendScenics = ref<API.ScenicVO[]>([])
const recommendRoutes = ref<API.RouteVO[]>([])
const loadingScenics = ref(false)
const loadingRoutes = ref(false)

// 模态框状态
const scenicDetailVisible = ref(false)
const routeDetailVisible = ref(false)
const selectedScenic = ref<API.ScenicVO | null>(null)
const selectedRoute = ref<API.RouteVO | null>(null)

// 获取推荐景点
const fetchRecommendScenics = async () => {
  try {
    loadingScenics.value = true
    const res = await getRecommendScenicList()

    if (res.data.code === 200) {
      recommendScenics.value = res.data.data?.slice(0, 8) || []
    } else {
      message.error('获取推荐景点失败')
    }
  } catch {
    message.error('获取推荐景点失败')
  } finally {
    loadingScenics.value = false
  }
}

// 获取推荐路线
const fetchRecommendRoutes = async () => {
  try {
    loadingRoutes.value = true
    const res = await getRecommendRouteList()

    if (res.data.code === 200) {
      recommendRoutes.value = res.data.data?.slice(0, 8) || []
    } else {
      message.error('获取推荐路线失败')
    }
  } catch {
    message.error('获取推荐路线失败')
  } finally {
    loadingRoutes.value = false
  }
}

// 模态框操作方法
const showScenicDetail = (scenic: API.ScenicVO) => {
  selectedScenic.value = scenic
  scenicDetailVisible.value = true
}

const showRouteDetail = (route: API.RouteVO) => {
  selectedRoute.value = route
  routeDetailVisible.value = true
}

// 页面跳转方法
const goToScenicDetail = (scenic: API.ScenicVO) => {
  showScenicDetail(scenic)
}

const goToRouteDetail = (route: API.RouteVO) => {
  showRouteDetail(route)
}

const viewMoreScenics = () => {
  router.push('/scenic')
}

const viewMoreRoutes = () => {
  router.push('/route')
}

// 页面加载时获取推荐数据
onMounted(() => {
  fetchRecommendScenics()
  fetchRecommendRoutes()
})
</script>
<style scoped>
.home-page {
  min-height: 100vh;
  background: #fafafa;
}

/* 轮播图区域 */
.hero-section {
  height: 400px;
  overflow: hidden;
}

/* 轮播图基础样式 */
:deep(.ant-carousel) {
  height: 100%;
}

:deep(.ant-carousel .slick-list) {
  height: 100%;
}

:deep(.ant-carousel .slick-track) {
  height: 100%;
}

/* 轮播图样式 */
:deep(.slick-slide) {
  height: 500px;
  text-align: center;
  background: #f5f5f5;
  overflow: hidden;
  display: flex !important;
  align-items: center;
  justify-content: center;
}

:deep(.slick-arrow.custom-slick-arrow) {
  width: 25px;
  height: 25px;
  font-size: 25px;
  color: #fff;
  background-color: rgba(31, 45, 61, 0.11);
  transition: ease all 0.3s;
  opacity: 0.3;
  z-index: 1;
}

:deep(.slick-arrow.custom-slick-arrow:before) {
  display: none;
}

:deep(.slick-arrow.custom-slick-arrow:hover) {
  color: #fff;
  opacity: 0.5;
}

:deep(.slick-slide h3) {
  color: #fff;
}

:deep(.slick-slide .ant-image) {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f5f5f5;
}

:deep(.slick-slide .ant-image img) {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
}

/* 欢迎区域 */
.welcome-section {
  padding: 60px 0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  text-align: center;
}

.welcome-title {
  font-size: 2.5rem;
  font-weight: 600;
  margin: 0 0 16px 0;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.welcome-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin: 0;
}

/* 推荐区域 */
.recommend-section {
  padding: 60px 0;
  background: white;
}

.recommend-section:nth-child(odd) {
  background: #fafafa;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
}

.section-title {
  font-size: 1.8rem;
  font-weight: 600;
  color: #262626;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

.section-title .anticon {
  color: #1890ff;
  font-size: 1.5rem;
}

/* 网格布局 - 每行4个，最多2行 */
.scenic-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
  max-height: calc(2 * (180px + 120px + 24px)); /* 限制最多2行 */
  overflow: hidden;
}

.route-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 24px;
  max-height: calc(2 * (180px + 140px + 24px)); /* 限制最多2行 */
  overflow: hidden;
}

/* 空状态和加载状态 */
.empty-state,
.loading-state {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 60px 0;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .container {
    padding: 0 16px;
  }

  .scenic-grid,
  .route-grid {
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
  }

  .welcome-title {
    font-size: 2rem;
  }

  .section-title {
    font-size: 1.5rem;
  }
}

@media (max-width: 768px) {
  .hero-section {
    height: 300px;
  }

  :deep(.slick-slide) {
    height: 300px;
  }

  .welcome-section {
    padding: 40px 0;
  }

  .welcome-title {
    font-size: 1.8rem;
  }

  .welcome-subtitle {
    font-size: 1rem;
  }

  .recommend-section {
    padding: 40px 0;
  }

  .section-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
    margin-bottom: 30px;
  }

  .scenic-grid,
  .route-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }
}

@media (max-width: 480px) {
  .container {
    padding: 0 12px;
  }

  .welcome-title {
    font-size: 1.5rem;
  }

  .section-title {
    font-size: 1.3rem;
  }
}
</style>
